package sk.tramtatiri.pages.users;

import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import sk.tramtatiri.MySession;
import sk.tramtatiri.WicketApplication;

public abstract class UserPageTemplate extends WebPage {

	private TextField<String> searchBoxField;

	public UserPageTemplate() {
		add(new Label("pageTitle",new PropertyModel(this,"pageTitle")));
		BookmarkablePageLink trackerLink = new BookmarkablePageLink("linkTrackerHome", TrackerHomePanel.class); 
		BookmarkablePageLink userSettingsLink = new BookmarkablePageLink("userSettingsLink", UserSettingsPage.class);
		BookmarkablePageLink loginLink = new BookmarkablePageLink("loginLink", LoginPage.class);
		BookmarkablePageLink registrationLink = new BookmarkablePageLink("registrationLink", RegistrationPage.class);
		Link logoutLink = new Link("logoutLink") {
			@Override
			public void onClick() {
				getSession().invalidateNow();
				getRequestCycle().setRedirect(true);
				setResponsePage(UserHomePage.class);
			}
		};
		Link messagesLink = new Link("messagesLink") {
			@Override
			public void onClick() {
				setResponsePage(MessagingPage.class);
			}
		};
		messagesLink.add(new Image("messageIcon", new ResourceReference(this.getClass(),"message_icon.png")));
		
		Label loggedUserLabel = new Label("loggedInNick", ((MySession)getSession()).getUserNickname());
		
		searchBoxField = new TextField<String>("globalSearchBox", new Model(""));
		searchBoxField.setRequired(true);
		
		Form<?> globalSearchForm = new Form<Void>("globalSearchForm") {
			@Override
			protected void onSubmit() {
				if(((MySession)getSession()).hasPermission()){
					PageParameters params = new PageParameters();
					params.add("name", searchBoxField.getInput());
					setResponsePage(UserSearchResultPage.class,params);
				}
			}
		};
		globalSearchForm.add(searchBoxField);
		
		if (((MySession)getSession()).hasPermission()) {
			trackerLink.setVisible(true);
			loggedUserLabel.setVisible(true);
			userSettingsLink.setVisible(true);
			logoutLink.setVisible(true);
			loginLink.setVisible(false);
			registrationLink.setVisible(false);
			messagesLink.setVisible(true);
		} else {
			trackerLink.setVisible(false);
			loggedUserLabel.setVisible(false);
			userSettingsLink.setVisible(false);
			logoutLink.setVisible(false);
			loginLink.setVisible(true);
			registrationLink.setVisible(true);
			messagesLink.setVisible(false);
		}
		
		add(trackerLink.setAutoEnable(true));
		add(loggedUserLabel);
		add(userSettingsLink);
		add(logoutLink);
		add(loginLink);
		add(registrationLink);
		add(messagesLink);
		add(globalSearchForm);
	}
	
	// To be overridden by "child" templates
	public abstract String getPageTitle();
}
